package com.nowcoder.community.util;

import com.nowcoder.community.entity.User;
import org.springframework.stereotype.Component;

/**
 * @author AnYang
 * @Description 起到容器的作用，持有用户信息，用来代替session对象
 * @Version 1.0.0
 * @Date 2024/1/18
 */

@Component
public class HostHolder {

    private ThreadLocal<User> users = new ThreadLocal<>();

    // 以线程为key，存取值；会先获取当前线程，通过当前线程获取对应map，并将值存入；取值时类似
    public void setUser(User user) {
        users.set(user);
    }

    public User getUser() {
        return users.get();
    }

    public void clear() {
        users.remove();
    }

}
